var app = getApp()
// pages/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contents: {},
    defult: 10,
    isPage: 1,
    navId: "",
    isEmpty: true,
  },

  //传递数据到发送页面
  onBatch: function (e) {
    var data = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/batch/batch?data=' + JSON.stringify(data) + "&class=" + 0,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showNavigationBarLoading();
    var getApiContUrl = app.globalData.smsbaoApi + "/batch/sapi/get_sms_content";
    var nav = JSON.parse(options.nav);
    this.setData({
      navName: nav,
      navId: nav.id,
    })
    //请求数据的参数
    var parameter = {
      cid: nav.id,
      num: this.data.defult,
      page: this.data.isPage,
      isChoice: 0
    };
    this.getApiContent(getApiContUrl, parameter);
    wx.startPullDownRefresh()
  },
  getApiContent: function (url, parameter) {
    let that = this;
    wx.request({
      url: url,
      data: parameter,
      method: "post",
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == '200') {
          var data = {};
          if (!that.data.isEmpty) {
            data = that.data.contents.concat(res.data.data);
          }
          else {
            data = res.data.data;
            that.data.isEmpty = false;
          }
          that.setData({
            contents: data
          })
          wx.hideNavigationBarLoading();
        }
      },
      fail: function (error) {
        console.log(error)
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    wx.setNavigationBarTitle({
      title: this.data.navName.name
    })
  },
  //下拉加载
  scrolltolower: function (event) {
    var getApiContUrl = app.globalData.smsbaoApi + "/get_sms_content";
    var nums = this.data.defult;
    var isPage = this.data.isPage += 1;
    var parameter = {
      cid: this.data.navId,
      num: nums,
      page: isPage,
      isChoice: 0
    };
    this.getApiContent(getApiContUrl, parameter)
  },
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function (e) {
    wx.stopPullDownRefresh()
  },
})
